<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/28
 * Time: 20:16
 */
declare(strict_types=1);

namespace App\Controller\MchApi;

use App\Constants\XZXResponseCode;
use App\Exception\XZXValidationException;
use App\Model\Enum\OrderHandleStatusEnum;
use App\Service\CommonService;
use App\Service\EmailCheckService;
use App\Utils\CustomUtils;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Middleware\MchApi\MchJWTAuthMiddleware;
use Hyperf\Contract\TranslatorInterface;

/**
 * Class CommonController
 * @package App\Controller
 *
 * @Controller(prefix="/mch/api/common")
 */
class CommonController extends MchBaseController
{
    /**
     * @Inject
     * @var TranslatorInterface
     */
    private $translator;
        
    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    /**
     * @Inject()
     * @var EmailCheckService
     */
    protected $EmailCheckService;

    /**
     * @Inject()
     * @var CommonService
     */
    protected $CommonService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct(LoggerFactory $loggerFactory)
    {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    private function setLocale()
    {
        if($this->request->header('locale') == 'en')
        {
            $this->translator->setLocale('en');
        }
    } 

    /**
     * 发送验证码
     * @PostMapping(path = "getEmailValidCode")
     */
    public function getEmailValidCode(RequestInterface $request)
    {
        $this->setlocale();

        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'email' => 'required|email',
                'type'  => 'required|integer',
            ]
        );

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();
        return $this->EmailCheckService->add($param);
    }

    /**
     * ali-oss临时凭证
     * @PostMapping(path = "aliOssTmpKey")
     */
    public function aliOssTmpKey(RequestInterface $request)
    {
        return $this->CommonService->aliOssTmpKey();
    }

    /**
     * 检查email是否可用注册
     * @PostMapping(path = "checkRegisterEmailValid")
     */
    public function checkRegisterEmailValid(RequestInterface $request)
    {
        $this->setlocale();

        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'email' => 'required|email',
            ]
        );

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();
        return $this->EmailCheckService->checkEmailValid($param);
    }

    /**
     * 检查email是否可用
     * @PostMapping(path = "checkEmailValid")
     * @Middleware(MchJWTAuthMiddleware::class)
     */
    public function checkEmailValid(RequestInterface $request)
    {
        $this->setlocale();

        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'email' => 'required|email',
            ]
        );

        if ($validator->fails()) {
            // Handle exception
            $errorMessage = $validator->errors()->first();
            throw new XZXValidationException($errorMessage);
        }

        $param = $request->all();
        return $this->EmailCheckService->checkEmailValidAndSettleCurrency($param);
    }

    /**
     * 审核步骤
     * @PostMapping(path = "handleStatusOption")
     * @Middleware(MchJWTAuthMiddleware::class)
     */
    public function handleStatusOption (RequestInterface $request) {
        $data = (new OrderHandleStatusEnum())->getOptions();
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE, XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), $data);
    }

}